package com.travel.travelnetwork.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.travel.travelnetwork.mapper.AdminMapper;
import com.travel.travelnetwork.mapper.CategoryMapper;
import com.travel.travelnetwork.mapper.RoleMapper;
import com.travel.travelnetwork.pojo.Admin;
import com.travel.travelnetwork.pojo.Permission;
import com.travel.travelnetwork.pojo.Role;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import com.travel.travelnetwork.bean.RoleWithStatus;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class AdminService {
    @Autowired
    private AdminMapper adminMapper;
    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private PasswordEncoder encoder;

    //分类查询管理员
    public Page<Admin> findPage(int currentPage, int pageSize) {
        Page selectPage = adminMapper.selectPage(new Page(currentPage, pageSize), null);
        return selectPage;
    }

    //新增管理员
    public void add(Admin admin) {
        admin.setPassword(encoder.encode(admin.getPassword()));
        adminMapper.insert(admin);
    }


    //根据id查询管理员
    public Admin findbyid (Integer aid) {
       return adminMapper.selectById(aid);
    }

    //修改管理员
    public void updata(Admin admin) {
        String oldpassword = adminMapper.selectById(admin.getAid()).getPassword();
        String newpassword = admin.getPassword();
        if (!encoder.matches(oldpassword, newpassword)) {
            admin.setPassword(encoder.encode(newpassword));
        }
        adminMapper.updateById(admin);
    }

    //查询管理员详情
    public Admin findDesc(Integer aid) {
        return adminMapper.findDesc(aid);
    }

    // 查询用户的角色情况
    public List<RoleWithStatus> findRole(Integer aid){
        // 查询所有角色
        List<Role> roles = roleMapper.selectList(null);
        // 查询用户拥有的所有角色id
        List<Integer> rids = roleMapper.findRoleIdByAdmin(aid);
        // 带有状态的角色集合
        List<RoleWithStatus> roleList = new ArrayList();
        // 遍历所有角色
        for (Role role:roles){
            // 创建带有状态的角色
            RoleWithStatus roleWithStatus = new RoleWithStatus();
            // 将角色属性复制给带有状态的角色
            BeanUtils.copyProperties(role,roleWithStatus);
            if(rids.contains(role.getRid())){ //用户拥有该角色，角色状态设置为true
                roleWithStatus.setAdminHas(true);
            }else { //用户不拥有该角色，角色状态设置为false
                roleWithStatus.setAdminHas(false);
            }
            roleList.add(roleWithStatus);
        }

        return roleList;

    }

    public void updateRoles(Integer aid,Integer[] ids){

        // 删除用户的所有角色
        adminMapper.deleteAdminAllRoles(aid);
        // 重新给用户添加角色
        if (ids!=null&&ids.length>0){
            for (Integer rid:ids){
                adminMapper.addAdminRole(aid,rid);
            }
        }
    }

    //修改用户状态
    public void upadtaStatus(Integer aid){
        Admin admin = adminMapper.selectById(aid);
        admin.setStatus(!admin.isStatus());
        adminMapper.updateById(admin);
    }

    // 根据名字查询管理员
    public Admin findByAdminName(String username) {
        QueryWrapper<Admin> queryWrapper = new QueryWrapper();
        queryWrapper.eq("username", username);
        Admin admin = adminMapper.selectOne(queryWrapper);
        return admin;

    }

    // 根据名字查询权限
    public List<Permission> findAllPermission(String username) {
                return adminMapper.findAllPermission(username);
    }

}
